<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <h2>毕业要求达成度评价系统</h2>
        </div>
      </template>
      
      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="0"
        size="large"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store'
import { ElMessage } from 'element-plus'
import request from '../api/request'

const router = useRouter()
const userStore = useUserStore()
const loading = ref(false)
const formRef = ref(null)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    loading.value = true
    
    console.log('发送登录请求:', {
      username: loginForm.username,
      password: loginForm.password
    })
    
    const res = await request.post('api/auth/login', {
      username: loginForm.username,
      password: loginForm.password
    })
    
    console.log('登录响应:', res)
    
    if (res.code === 200) {
      let { token, user, userRoleCode } = res.data
      console.log('登录成功，原始token:', token)

      // 移除Bearer前缀再存储
      if (token.startsWith('Bearer ')) {
        token = token.substring(7)
      }
      console.log('登录成功，去除Bearer后的token:', token)
      
      userStore.setToken(token)
      userStore.setUserInfo(user)
      userStore.setUserRole(userRoleCode)
      ElMessage.success('登录成功')
      router.push('/dashboard/profile')
    } else {
      console.error('登录失败:', res)
      ElMessage.error(res.message || '登录失败')
    }
  } catch (error) {
    console.error('登录异常:', error)
    console.error('错误响应:', error.response)
    ElMessage.error(error.response?.data?.message || '用户名或密码错误')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.login-card {
  width: 400px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  text-align: center;
  padding: 20px 0;
}

.card-header h2 {
  margin: 0;
  color: #303133;
  font-size: 24px;
}

.login-form {
  padding: 20px 30px;
}

.login-form :deep(.el-form-item) {
  margin-bottom: 25px;
}

.login-form :deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
  padding: 0 15px;
  height: 45px;
}

.login-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #409eff inset;
}

.login-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409eff inset;
}

.login-form :deep(.el-input__inner) {
  height: 45px;
  font-size: 16px;
}

.login-form :deep(.el-input__prefix) {
  font-size: 18px;
  color: #909399;
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  margin-top: 10px;
}
</style> 